home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swaga_c.zip / CMDLINE.SWG / 0014_Command Line Parsing.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  4KB  |  148 lines

  1. {
  2.    CMDPARSE.PAS
  3.    Command line parameter parsing
  4.    If unit is included, command line is automatically parsed.
  5.  
  6. }
  7. unit CmdParse;
  8. interface
  9.  
  10. uses
  11.   Strings;
  12.  
  13. type
  14.  
  15.   CmdPtr = ^CmdRec;
  16.   CmdRec = Record
  17.     CmdParam : String[64];  { 64 char to allow for maximum path length }
  18.     Next     : CmdPtr;
  19.   end;
  20.  
  21. var
  22.   FirstCmd   : CmdPtr;  { Ptr to first CmdRec or Nil }
  23.   CurrentCmd : CmdPtr;  { Ptr to current CmdRec or Nil }
  24.   CmdCnt     : Byte;    { Total of all entered parameters }
  25.  
  26. function FirstCmdStr : String;
  27.   { Returns the first parameter entered, or the null ('') string. }
  28.  
  29. function NextCmdStr : String;
  30.   { Returns the next parameter entered, or the null ('') string. }
  31.  
  32. function ValidCmdStr(ChkStr : String; CaseCheck : Boolean) : Boolean;
  33.   { Checks all input parameters to see if CmdStr has been input.  CaseCheck
  34.     determines if the the search of parameters should be case sensitive. }
  35.  
  36. procedure ClearCmd;
  37.   { Disposes of all command line pointers except FirstCmd & CurrentCmd
  38.     which are set to Nil }
  39.  
  40. implementation
  41. {---------------------------------------------------------------------------}
  42. var
  43.   Cmd_I : Byte;
  44.  
  45.   function FirstCmdStr : String;
  46.     { Returns the first parameter entered, or the null ('') string. }
  47.   begin
  48.     if FirstCmd <> Nil then begin
  49.       FirstCmdStr := FirstCmd^.CmdParam;
  50.       CurrentCmd := FirstCmd^.Next;
  51.     end else
  52.       FirstCmdStr := '';
  53.   end;
  54.  
  55.   function NextCmdStr : String;
  56.     { Returns the next parameter entered, or the null ('') string. }
  57.   begin
  58.     if CurrentCmd <> Nil then begin
  59.       NextCmdStr := CurrentCmd^.CmdParam;
  60.       CurrentCmd := CurrentCmd^.Next;
  61.     end else
  62.       NextCmdStr := '';
  63.   end;
  64.  
  65.   function ValidCmdStr(ChkStr : String; CaseCheck : Boolean) : Boolean;
  66.     { Checks all input parameters to see if CmdStr has been input.  CaseCheck
  67.       determines if the the search of parameters should be case sensitive. }
  68.   var
  69.     CmdStr : String;
  70.     FoundCmd : Boolean;
  71.   begin
  72.     CmdStr := FirstCmdStr;
  73.     FoundCmd := False;
  74.     repeat
  75.       if CaseCheck then
  76.       begin
  77.         if CmdStr = ChkStr then
  78.           FoundCmd := True
  79.       end
  80.       else
  81.         if (StUpCase(CmdStr) = StUpCase(ChkStr)) then
  82.           FoundCmd := True;
  83.       CmdStr := NextCmdStr;
  84.     until CmdStr = '';
  85.     ValidCmdStr := FoundCmd;
  86.   end;
  87.  
  88.   procedure ClearCmd;
  89.     { Disposes of all command line pointers except FirstCmd & CurrentCmd
  90.       which are set to Nil }
  91.   begin
  92.     if FirstCmd <> Nil then
  93.       repeat
  94.         CurrentCmd := FirstCmd^.Next;
  95.         Dispose(FirstCmd);
  96.         FirstCmd := CurrentCmd;
  97.       until FirstCmd = Nil;
  98.   end;
  99.  
  100.   procedure CmdAdd(CmdStr : String);
  101.     { Add a new CmdRec to the list }
  102.   var
  103.     TempCmdPtr : CmdPtr;
  104.   begin
  105.     New(TempCmdPtr);
  106.     if FirstCmd = Nil then
  107.       FirstCmd := TempCmdPtr
  108.     else
  109.       CurrentCmd^.Next := TempCmdPtr;
  110.     TempCmdPtr^.Next := Nil;  { Initialize new Next pointer }
  111.     TempCmdPtr^.CmdParam := CmdStr;
  112.     CurrentCmd := TempCmdPtr;
  113.     Inc(CmdCnt);
  114.   end;
  115.  
  116.   procedure ParsePStr(PStr : String);
  117.     { Parse out a ParamStr() into multiple CmdRecs }
  118.   var
  119.     WorkStr   : String;
  120.     TempStr   : String;
  121.     SpPos: Byte;
  122.     I,L : Byte;
  123.   begin
  124.  
  125.     { translate first - to / }
  126.     if PStr[1] = '-' then
  127.       PStr[1] := '/';
  128.  
  129.     SpPos := Pos('/',Copy(PStr,2,Length(PStr)-1));
  130.     if SpPos > 0 then
  131.       repeat
  132.         CmdAdd(Copy(PStr,1,SpPos));
  133.         PStr := Copy(PStr,SpPos+1,Length(PStr)-SpPos);
  134.         SpPos := Pos('/',Copy(PStr,2,Length(PStr)-1));
  135.       until SpPos = 0;
  136.     CmdAdd(PStr);
  137.   end;
  138.  
  139. begin
  140.   FirstCmd := Nil;
  141.   CurrentCmd := Nil;
  142.   CmdCnt := 0;
  143.  
  144.   for Cmd_I := 1 to ParamCount do
  145.     ParsePStr(ParamStr(Cmd_I));
  146.   CurrentCmd := FirstCmd;
  147. end.
  148.